1 module hip.event.handlers.inputmap; 2 import hip.util.reflection; 3 import hip.data.jsonc; 4 import hip.filesystem.hipfs; 5 import hip.error.handler; 6 import hip.api.input; 7 import hip.event.api; 8 9 class HipInputMap : IHipInputMap 10 { 11 alias Context = IHipInputMap.Context; 12 Context[string] inputMapping; 13 ubyte id; 14 //registerInputAction("menu", MOUSE_BTN_R, TOUCH_0 | TOUCH_1, KEY_WINDOWS) 15 private pragma(inline) Context* getAction(string actionName) 16 { 17 Context* ctx = actionName in inputMapping; 18 if(ctx == null) 19 { 20 ErrorHandler.showErrorMessage("HipInputMap action getter error", 21 '"'~actionName~"' does not exists on input mapping"); 22 } 23 return ctx; 24 } 25 float isActionPressed(string actionName) 26 { 27 Context* c = getAction(actionName); 28 if(!c) return 0.0f; 29 float greatest = 0; 30 foreach(g; c.btns) if(isGamepadButtonPressed(g, id)) 31 greatest = 1.0f; 32 foreach(k; c.keys) if(isKeyPressed(k, id)) 33 greatest = 1.0f; 34 return greatest; 35 } 36 float isActionJustPressed(string actionName) 37 { 38 Context* c = getAction(actionName); 39 if(!c) return 0.0f; 40 float greatest = 0; 41 foreach(g; c.btns) if(isGamepadButtonJustPressed(g, id)) 42 greatest = 1.0f; 43 foreach(k; c.keys) if(isKeyJustPressed(k, id)) 44 greatest = 1.0f; 45 return greatest; 46 } 47 float isActionJustReleased(string actionName) 48 { 49 Context* c = getAction(actionName); 50 if(!c) return 0.0f; 51 float greatest = 0; 52 foreach(g; c.btns) if(isGamepadButtonJustReleased(g, id)) 53 greatest = 1.0f; 54 foreach(k; c.keys) if(isKeyJustReleased(k, id)) 55 greatest = 1.0f; 56 return greatest; 57 } 58 59 void registerInputAction(string actionName, Context ctx) 60 { 61 mixin(ErrorHandler.assertReturn!q{actionName != ""}("Register Input Action should contain a name")); 62 inputMapping[actionName] = ctx; 63 64 } 65 66 @ExportD("Mem") static IHipInputMap parseInputMap(ubyte[] file, string fileName, ubyte id = 0) 67 { 68 HipInputMap ret = new HipInputMap(); 69 JSONValue inputJson = parseJSON(cast(string)file); 70 71 JSONValue* temp = ("actions" in inputJson.object); 72 ErrorHandler.assertLazyErrorMessage(temp != null, "HipInputMap wrong formatting", 73 "\"actions\" not found in "~fileName); 74 75 foreach(k, v; temp.object) 76 { 77 string actionName = k; 78 JSONValue* kb = ("keyboard" in v.object); 79 JSONValue* gp = ("gamepad" in v.object); 80 81 Context ctx; 82 if(kb != null) 83 { 84 foreach(key; kb.array) //Keyboard 85 ctx.keys~= key.str[0]; 86 } 87 if(gp != null) 88 { 89 foreach(btn; gp.array) //Gamepad 90 ctx.btns ~= gamepadButtonFromString(btn.str); 91 } 92 ret.inputMapping[actionName] = ctx; 93 ctx.name = actionName; 94 } 95 return ret; 96 } 97 } 98 99 100 mixin ExportDFunctions!(hip.event.handlers.inputmap);